package org.xq.shop.maxaishop.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.xq.shop.maxaishop.entity.XqAdmin;
import org.xq.shop.maxaishop.entity.XqCustomer;
import org.xq.shop.maxaishop.service.XqAdminService;
import org.xq.shop.maxaishop.util.JsonRes;
import org.xq.shop.maxaishop.util.Result;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (XqAdmin)表控制层
 *
 * @author makejava
 * @since 2021-06-11 18:17:22
 */
@Api( tags = "管理员管理")
@RestController
@RequestMapping("xqAdmin")
public class XqAdminController {
    /**
     * 服务对象
     */
    @Resource
    private XqAdminService xqAdminService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("根据主键查询单个系统用户")
    @ApiImplicitParam(name = "id", value = "用户ID", required=true,paramType="query" )
    @GetMapping("selectOne")
    public XqAdmin selectOne(Integer id) {
        return this.xqAdminService.queryById(id);
    }

    /**
     * 添加分管
     * @param xqAdmin
     * @return
     */
    @ApiOperation("这是添加接口")
    @ApiImplicitParam(name="xqAdmin",value="秒杀对象",required=true,paramType="body")
    @PostMapping("regist")
    public Map registered(@RequestBody XqAdmin xqAdmin){
        XqAdmin insert = this.xqAdminService.insert(xqAdmin);
//        System.out.println(insert);
       if (insert!=null){
           return JsonRes.success(insert);
       }else {
           return JsonRes.failure(null);
       }
    }

    @ApiOperation("管理员列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="页面",required=true,paramType="query"),
            @ApiImplicitParam(name="limit",value="每页行数",required=true,paramType="query"),
            @ApiImplicitParam(name="aname",value="用户名",paramType="query",dataType="String")
    })
    @GetMapping("selectAll")
    public Result<XqCustomer> selectAll(Integer page, Integer limit, String aname){
        int start = (page-1)*limit;
        Result<XqCustomer> result = new Result<XqCustomer>();
        if (aname==null){
            List<XqCustomer> xqCustomers = this.xqAdminService.selectAll(start, limit);
            int count = this.xqAdminService.selectCount();
//            System.out.println(count);
            result.setCode(200);
            result.setCount(count);
            result.setMsg("请求成功");
            result.setData(xqCustomers);

        }else if(aname=="") {
            List<XqCustomer> xqCustomers = this.xqAdminService.selectAll(start, limit);
            int count = this.xqAdminService.selectCount();
//            System.out.println(count);
            result.setCode(200);
            result.setCount(count);
            result.setMsg("请求成功");
            result.setData(xqCustomers);
        } else {
                List<XqCustomer> xqCustomers = this.xqAdminService.selectByUname(aname, start, limit);
                int count = this.xqAdminService.countt(aname);
                result.setCode(200);
                result.setCount(count);
                result.setMsg("请求成功");
                result.setData(xqCustomers);
            }
        return  result;
    }
    @ApiOperation("这是修改系统用户信息接口")
    @PostMapping("update")
    public Map update(@RequestBody XqAdmin xqAdmin){
        int update = this.xqAdminService.update(xqAdmin);
        if (update==1){
            return JsonRes.success(update);
        }else {
            return JsonRes.failure(null);
        }
    }
    @ApiOperation("删除系统用户")
    @ApiImplicitParam(name = "aid", value = "用户ID", required=true,paramType="query" )
    @GetMapping("deleteById")
    public boolean deleteById(int aid){
        return this.xqAdminService.deleteById(aid);
    }

    @ApiOperation("通过用户名查询")
    @ApiImplicitParam(name = "username", value = "用户名", required=true,paramType="query" )
    @PostMapping("selectByAname")
    public XqAdmin selectByAname(String username){
        return this.xqAdminService.selectByAname(username);
    }

}